<?php if (!defined("_ECRIRE_INC_VERSION")) return; if (class_exists('PdkAuteurModel')) return;
/**
 * SPIP Plugin Development Kit
 *
 * @author     Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @license    GNU/GPL
 * @package    plugins
 * @subpackage pdk
 * @category   outils
 * @copyright  (c) Aleksey Yaroshchuk 2011-2012
 * @link       http://www.spip-contrib.net/Developmet-Kit
 * @version    1.0.0
 */

include_spip('classes/pdk_model');
/** 
 *  This is PDK SPIP Article model
 *
 *  @author Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 */
class PdkAuteurModel extends pdk_Model {
    public function __construct() {
        parent::__construct();
        $this->config_model();        
    }
    
    function config_model() {
        // Define main table:
        $this->main_table = 'spip_auteurs';
        
        // Define primary key:
        $this->primary_key = 'id_auteur';
        
        // Define main table fields:
        $this->main_fields['id_auteur'] = 'id_auteur'; 
        $this->main_fields['nom'] = 'nom'; 
        $this->main_fields['bio'] = 'bio';
        $this->main_fields['email'] = 'email';
        $this->main_fields['nom_site'] = 'nom_site';
        $this->main_fields['url_site'] = 'url_site';
        $this->main_fields['login'] = 'login';
        $this->main_fields['pass'] = 'pass';
        $this->main_fields['low_sec'] = 'low_sec';       
        $this->main_fields['statut'] = 'statut';
        $this->main_fields['webmestre'] = 'webmestre';
        $this->main_fields['maj'] = 'maj';
        $this->main_fields['pgp'] = 'pgp';
        $this->main_fields['htpass'] = 'htpass';
        $this->main_fields['en_ligne'] = 'en_ligne';
        $this->main_fields['imessage'] = 'imessage';
        $this->main_fields['messagerie'] = 'messagerie';
        $this->main_fields['alea_actuel'] = 'alea_actuel';
        $this->main_fields['alea_futur'] = 'alea_futur';
        $this->main_fields['prefs'] = 'prefs';
        $this->main_fields['cookie_oubli'] = 'cookie_oubli';
        $this->main_fields['source'] = 'source';
        $this->main_fields['lang'] = 'lang';
        $this->main_fields['extra'] = 'extra';
                                                                                 
         // Define validation rules:
         $this->validation->set_rules( 
            array(
               array(
                     'field'   => 'email',
                     'label'   => 'Email',
                     'rules'   => ''
                  )
            )
         );                                     
    }
    
    /**
     *  Overload parent insert function to add pass generation       
     */
    function insert($data, $clear_id = false) {
        if (!$data['source']) $data['source'] = 'spip'; 
        $id_auteur = parent::insert($data, $clear_id);
        $pass = $data['pass']?$data['pass']:'';
        $this->update_pass($id_auteur, $pass);
        return $id_auteur;        
    }
    
    /**
     *  Overload parent update function to add pass generation       
     */
    function update($where, $data) {
        if (!$data['source']) $data['source'] = 'spip';
        parent::update($where, $data);
        $auteurs = $this->select($where);
        $pass = $data['pass']?$data['pass']:'';
        foreach ($auteurs as $aut) {
            $this->update_pass($aut['id_auteur'], $pass);  
        }
    }
    
    /**
     *  Update auteurs password. Auteur must exist in DB!
     */
    function update_pass($id_auteur, $pass) {
        include_spip('action/editer_auteur');
        instituer_auteur($id_auteur, array('pass'=>$pass));
    }
    
     function CreateLoginFromName($name) {
        include_spip('inc/charsets');
        $login = translitteration($name);
        $login = preg_replace('/\s/','',$login);
        return $login;
    }
    
    function CreatePassword($length=7) {
        $arr = array('a','b','c','d','e','f',
                 'g','h','i','j','k','l',
                 'm','n','o','p','r','s',
                 't','u','v','x','y','z',
                 'A','B','C','D','E','F',
                 'G','H','I','J','K','L',
                 'M','N','O','P','R','S',
                 'T','U','V','X','Y','Z',
                 '1','2','3','4','5','6',
                 '7','8','9','0');
        $pass = "";
        for($i = 0; $i < $length; $i++)
        { 
            $index = rand(0, count($arr) - 1);
            $pass .= $arr[$index];
        }
        return $pass;
    }
    
    function CheckAuteurExist($login='', $email='') {
        if ($login) {
            if (sql_countsel('spip_auteurs',"login='".$login."'")>0)
                return true;
        }
        if ($email) {
            if (sql_countsel('spip_auteurs',"email='".$email."'")>0)
                return true;
        }
        return false;
    }
}
?>